// data interpolated from vertex program output
struct vertexIN
{
  float3 objectSpacePos : TEXCOORD1;
  float3 normal   : TEXCOORD2;
};

float4 main(vertexIN vIn, 
              uniform float3 Ia,
              uniform float3 Id,
              uniform float3 Is,
              uniform float4 lightPosition1,
              uniform float4 lightPosition2,
              uniform float4 eyePosition,
              uniform float3 Ka,
              uniform float3 Kd,
              uniform float3 Ks,
              uniform float  shininess,
              uniform float4x4 modelViewInv) : COLOR
{
	// eye position:
	float3 ePos = mul(modelViewInv, eyePosition).xyz;

	// light positions:
	float3 lPos1 = lightPosition1.xyz;
	float3 lPos2 = lightPosition2.xyz;
	
	// direction to eye:
	float3 V = normalize(ePos - vIn.objectSpacePos);
	
	// directions to lights:
	float3 L1 = normalize(lPos1 - vIn.objectSpacePos);
	float3 L2 = normalize(lPos2 - vIn.objectSpacePos);
	
	float4 lightVals1 = lit(dot(vIn.normal, L1), dot(vIn.normal, normalize(L1 + V)), shininess);
	float4 lightVals2 = lit(dot(vIn.normal, L2), dot(vIn.normal, normalize(L2 + V)), shininess);

	float4 lightVals = lightVals1 + lightVals2;
	lightVals /= lightVals.w;
	
	float4 color;
	color.rgb = lightVals.x*Ia*Ka
			  + lightVals.y*Id*Kd
			  + lightVals.z*Is*Ks;  
	color.a = 1;
	return color;  
}